package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.feign.BehaviorFeign;
import com.heima.article.feign.UserFeign;
import com.heima.article.mapper.ApCollectionBehaviorMapper;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.AppArticleInfoService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class ApArticleInfoServiceImpl implements AppArticleInfoService {
    @Autowired
    private ArticleConfigMapper articleConfigMapper;
    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        Map map = new HashMap();
        if (dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticleConfig apArticleConfig = articleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if (apArticleConfig == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (!apArticleConfig.getIsDown() && !apArticleConfig.getIsDelete()){
            ApArticleContent apArticleContent = articleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
            map.put("content" , apArticleContent);
        }
        map.put("config" , apArticleConfig);
        return ResponseResult.okResult(map);
    }

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Autowired
    private ApCollectionBehaviorMapper apCollectionBehaviorMapper;

    @Autowired
    private AuthorMapper authorMapper;

    @Autowired
    private UserFeign userFeign;

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        //判断参数
        if (dto == null && dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }
        //获得实体
        ApUser apUser = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean isUnlike=false,isLike = false,isCollection = false,isFollow = false;
        //判断点赞行为
        ApLikesBehavior apLikesBehavior = behaviorFeign.findLikeByArticleIdAndEntryId(dto.getArticleId(), apBehaviorEntry.getId(), ApLikesBehavior.Type.ARTICLE.getCode());
        if (apLikesBehavior != null && apLikesBehavior.getType() == ApLikesBehavior.Type.ARTICLE.getCode()){
            isLike = true;
        }
        //查询不喜欢行为
        ApUnlikesBehavior apUnLikesBehavior = behaviorFeign.findUnLikeByArticleIdAndEntryId(dto.getArticleId(), apBehaviorEntry.getId());
        if (apUnLikesBehavior != null && apUnLikesBehavior.getType() == ApUnlikesBehavior.Type.UNLIKE.getCode()){
            isUnlike = true;
        }
        //判断收藏行为
        ApCollection apCollection = apCollectionBehaviorMapper.selectOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getArticleId, dto.getArticleId())
                .eq(ApCollection::getEntryId, apBehaviorEntry.getId())
                .eq(ApCollection::getType, ApCollection.Type.ARTICLE.getCode()));
        if (apCollection != null && apCollection.getOperation() == 0){
            isCollection = true;
        }
        //判断关注行为
        //文章作者信息表
        ApAuthor apAuthor = authorMapper.selectById(dto.getAuthorId());
        ApUserFollow apUserFollow = userFeign.finduserIdAndFollowId(apUser.getId(), apAuthor.getUserId());
        if (apUserFollow != null){
            isFollow = true;
        }
        //返回
        Map<String , Object> map = new HashMap<>();
        map.put("isUnlike",isUnlike);
        map.put("isLike",isLike);
        map.put("isCollection" , isCollection);
        map.put("isFollow" , isFollow);
        return ResponseResult.okResult(map);
    }
}
